<?php

abstract class MapperRegistry
{
    private static $_memory = array();
    public static $_Phlameregistry = array();
    
    /**
     * @param string $id
     * @param Mapper $Mapper
     */
    public static function set($id, Mapper $Mapper)
    {
        if (isset(self::$_memory[$id])) {
            throw new RegistryException($id.' already set!');
        }
        self::$_memory[$id] = $Mapper;
    }
    
    /**
     * @param string $id
     * @return Mapper
     */
    public static function get($id)
    {
        if (!isset(self::$_memory[$id])) {
            throw new RegistryException('ID not found');
        }
        return self::$_memory[$id];
    }
    
    public static function register(Phlame $Phlame)
    {
        $class = get_class($Phlame);
        if (!isset(self::$_Phlameregistry[$class])) {
            self::$_Phlameregistry[$class] = array();
        }
        self::$_Phlameregistry[$class][$Phlame->getID()] = $Phlame;
    }
    
    /**
     * Clean the registry, drop all saved Mappers
     *
     */
    public static function cleanUp()
    {
        identify(self::$_Phlameregistry);
        foreach (self::$_Phlameregistry as &$class) {
            foreach ($class as &$Phlame) {
                $Phlame->close();
                $Phlame = NULL;
            }
        }
        self::$_Phlameregistry = array();
        
        identify(self::$_memory);
        foreach (self::$_memory as &$Mapper) {
            $Mapper->update();
            $Mapper = NULL;
        }
        self::$_memory = array();
    }
}
register_shutdown_function('MapperRegistry::cleanUp');